package com.sakura.mall.product.spuInfoDesc.service.impl;

import com.sakura.mall.common.response.Page;
import com.sakura.mall.product.spuInfoDesc.domain.entity.SpuInfoDescEntity;
import com.sakura.mall.product.spuInfoDesc.domain.request.AddSpuInfoDescRequest;
import com.sakura.mall.product.spuInfoDesc.domain.request.UpdateSpuInfoDescRequest;
import com.sakura.mall.product.spuInfoDesc.domain.request.SpuInfoDescPageRequest;
import com.sakura.mall.product.spuInfoDesc.domain.vo.SpuInfoDescVo;
import com.sakura.mall.product.spuInfoDesc.mapper.SpuInfoDescMapper;
import com.sakura.mall.product.spuInfoDesc.service.SpuInfoDescService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * spu信息介绍 service impl
 *
 * @author sakura
 */
@Service
@RequiredArgsConstructor
public class SpuInfoDescServiceImpl extends ServiceImpl<SpuInfoDescMapper, SpuInfoDescEntity> implements SpuInfoDescService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSpuInfoDescRequest request) {
        // todo 其他校验逻辑
        SpuInfoDescEntity spuInfoDesc = request.convert(SpuInfoDescEntity.class);
        this.save(spuInfoDesc);
    }

    @Override
    public Page<SpuInfoDescVo> queryPage(SpuInfoDescPageRequest request) {
        IPage<SpuInfoDescEntity> qp = request.toPage();
        LambdaQueryWrapper<SpuInfoDescEntity> queryWrapper = new LambdaQueryWrapper<>();
        this.page(qp, queryWrapper);
        IPage<SpuInfoDescVo> ret = qp.convert(entity -> entity.convert(SpuInfoDescVo.class));
        return Page.convert(ret);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateById(Long id, UpdateSpuInfoDescRequest request) {
        SpuInfoDescEntity entity = request.convert(SpuInfoDescEntity.class);
        entity.setSpuId(id);
        this.updateById(entity);
    }

}

